package com.frank.loadinglibrary.common.search;

import com.frank.loadinglibrary.common.element.MagnifyElement;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.StackLayout;
import ohos.app.Context;

public class SearchLoadingComponent extends StackLayout {

    private MagnifyComponent magnifyComponent;

    private int padding = 20;

    String pillarColor = "#0000ff";
    String glassColor = "#00ff00";

    private int duration = 3000;

    public SearchLoadingComponent setDuration(int duration) {
        this.duration = duration;
        return this;
    }

    public SearchLoadingComponent setPillarColor(String pillarColor) {
        this.pillarColor = pillarColor;
        return this;
    }

    public SearchLoadingComponent setGlassColor(String glassColor) {
        this.glassColor = glassColor;
        return this;
    }

    public SearchLoadingComponent(Context context) {
        this(context, null);
    }

    public SearchLoadingComponent(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public SearchLoadingComponent(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initUI();
    }

    private void initUI() {
        magnifyComponent = new MagnifyComponent(getContext());
        setPadding(padding, padding, padding, padding);
        setBindStateChangedListener(new BindStateChangedListener() {
            @Override
            public void onComponentBoundToWindow(Component component) {
                if (magnifyComponent == null) {
                    magnifyComponent = new MagnifyComponent(component.getContext());
                    magnifyComponent.config(new MagnifyElement.MagnifyBuilder(magnifyComponent).setDurtion(duration).setPadding(padding).setGlassColor(glassColor).setPillarColor(pillarColor).build());
                    addComponent(magnifyComponent);
                }
            }

            @Override
            public void onComponentUnboundFromWindow(Component component) {
                //do nothing
            }
        });
    }


}
